package com.mybatis.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mybatis.Dao.IMemberMapper;
import com.mybatis.Util.MyBatisUtil;
import com.mybatis.Util.Result;
import com.mybatis.entity.Employee;

/**
 * Servlet implementation class delEmployee
 */
@WebServlet("/delEmployee")
public class delEmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public delEmployeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");//设置输出信息编码方式为utf-8
		response.setContentType("text/json");//设置输出信息格式
		String idStr = request.getParameter("id");//获取要删除的员工编号
		//判读获取到的员工编号是否为空，不为空时传入
		if(idStr!=null&&!idStr.equals("")) {
			int id = Integer.parseInt(idStr);//将获取到的id转为整形
			//调用删除方法
			IMemberMapper mdao = MyBatisUtil.getMapper(IMemberMapper.class);
			int num = mdao.delete(id);
			//将执行结果存入结果集
			Result rs = new Result(num,"成功",num,null);
			//将结果集转为json格式
			Gson g= new Gson();
			String json = g.toJson(rs);
			//输出结果集
			PrintWriter out =response.getWriter();
			out.print(json);
			//释放资源
			out.close();
			MyBatisUtil.closeSqlSession();
		}else {
			//获取不到工号返回错误信息
			Result rs = new Result(-1,"删除失败",-1,null);
			Gson g= new Gson();
			String json = g.toJson(rs);
			PrintWriter out =response.getWriter();
			out.print(json);
			out.close();
			MyBatisUtil.closeSqlSession();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
